package test_achieve.web;
import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import org.junit.Assert;
import org.openqa.selenium.By;
import util.tools.CommonTools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chenkuo on 2018/1/18.
 */
public class CouponWeb {
    @Given("^优惠券已被消费$")
    public void 优惠券已被消费() throws Throwable {
        OrderMain bb = new OrderMain();
        bb.w进入我的闪送中();
        int num =4;
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"container_wrapper\"]/div[1]/div[1]/dl[2]/dd[3]/a")).click();
        String abc = CommonTools.getText(Constant.driverWeb,"className","coupon-count");//获取元素内容
        String count = CommonTools.cutMiddle(abc,"剩余","张");//字符串截取
        Assert.assertEquals(4,count);//断言

    }


    @Given("^w进入优惠券中判断优惠券类型与金额：(.*)：(.*)$")
    public void w进入优惠券中判断优惠券类型与数量(String type,String money) throws Throwable {
        w点击优惠券菜单();
        w判断优惠券类型与金额(type,money);

    }
    @Given("^w点击优惠券菜单$")
    public void w点击优惠券菜单() throws Throwable {
        Constant.driverWeb.findElement(By.linkText("优惠券")).click();//点击优惠券菜单
    }
    @Given("^w判断优惠券类型与金额$")
    public void w判断优惠券类型与金额(String type,String money) throws Throwable {
        String typePage = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div[3]/div/div[2]")).getText();
        System.out.println("优惠券类型:"+typePage);
        Assert.assertEquals(type,typePage);
        String moneyPage = Constant.driverWeb.findElement(By.className("coupon-value")).getText();
        System.out.println("优惠券金额:"+moneyPage);
        Assert.assertEquals(money,moneyPage);

    }


    @Given("^w确认优惠券已使用：(.*)：(.*)$")
    public void w确认优惠券已使用(String type,String money) throws Throwable {
        String moneyPage1 ="";
        String typePage = Constant.driverWeb.findElement(By.className("c-type")).getText();
        System.out.println("券类型:"+typePage);
        Assert.assertEquals(type,typePage);
        String moneyPage = Constant.driverWeb.findElement(By.className("c-amount")).getText();
        if (moneyPage.contains("￥")){
             moneyPage1 = CommonTools.cutEnd(moneyPage,"￥");
        }else {
            moneyPage1 = CommonTools.cutFront(moneyPage,"折");
        }
        Assert.assertEquals(money,moneyPage1);
    }

    @Given("^w确认优惠券未使用$")
    public void w确认优惠券未使用() throws Throwable {
        String btn = Constant.driverWeb.findElement(By.xpath("//*[@id=\"coupon-body\"]/span[1]")).getText();
        System.out.println("33333"+btn);
        Assert.assertEquals("暂无可用优惠券",btn);//页面没有优惠券元素
    }

    @Given("^w判断优惠券张数：(.*)$")
    public void w判断优惠券张数张数(String coun) throws Throwable {
        String count = Constant.driverWeb.findElement(By.className("coupon-count")).getText();
        Assert.assertEquals(coun, CommonTools.cutMiddle(count,"剩余","张"));
    }

    @Given("^w判断优惠券有效期：(.*)$")
    public void w判断优惠券有效期(int day) throws Throwable {
        String data =Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div[3]/div/div[3]")).getText();
        Assert.assertEquals(data,CommonTools.getDateCount(day));
    }

    public static void main(String[] args) throws ParseException {

    }
}
